package com.shang.activemq.persistent;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.IOException;

import static javax.jms.Session.AUTO_ACKNOWLEDGE;

public class JmsConsumer_Topic2 {
    public static final String ACTIVEMQ_URL = "tcp://192.168.0.113:61616";
    public static final String TOPIC_NAME = "topic-persistent";

    public static void main(String[] args) throws JMSException, IOException {
        //1.创建连接工厂,按照给定的url地址，采用默认用户名和密码
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        //2.通过连接工厂，获得connection并启动访问
        Connection connection = activeMQConnectionFactory.createConnection();
        connection.setClientID("zs");
        //3.创建回话session
        //参数：事务 签收
        Session session = connection.createSession(false, AUTO_ACKNOWLEDGE);
        //4.创建目的地（具体是队列还是主题topic）
        Topic topic = session.createTopic(TOPIC_NAME);
        TopicSubscriber topicSubscriber = session.createDurableSubscriber(topic, "remark");
        connection.start();

        Message message = topicSubscriber.receive();
        while (null != message){
            TextMessage textMessage = (TextMessage) message;
            System.out.println("************topic: " + textMessage.getText());
            message = topicSubscriber.receive(5000L);
        }

        session.close();
        connection.close();
    }
}
